/*  BEGIN_FILE_HDR
******************************************Copyright(C)*****************************************
*
*                                       YKXH  Technology
*
***********************************File Info***************************************************
*   File Name       @: App_Nm.c
************************************************************************************************
*   Project/Product @:
*   Title           @:
*   Author          @: donglin.Zhang
************************************************************************************************
*   Description     @:
*
************************************************************************************************
*   Limitations     @: None
*
************************************************************************************************
*
************************************************************************************************
*   Revision History:
*
*    Version    Date          Initials       CR#             Descriptions
*   ---------   ----------    -------------  ----------    ---------------
*   1.0         2018/05/30    donglin.zhang   N/A          Original
*   1.1         2019/04/01    zhongliang.Yang N/A          OsekNm190401-01
*
************************************************************************************************
* END_FILE_HDR*/


#include "..\src\BSW\CommS\NM\YKBase\BaseComm.h"

#if(STD_ON == USE_OSEKNM_MODULE)
uint8 gNm_SleepFlag;
uint8 gNmBusoffFlag;
uint8 gNmBusOffConfirmFlag;
uint8 gCanNm_CanRxAppFlag ;
uint8 gCanNm_CanTxAppFlag=CAN_STOP;
uint16 TAppMessageDelyTimer;
extern uint8 nubNmBusOffCntAry[1];
extern uint8 isNmTxSuccessFlg;
extern uint8 bottv_localwakesignal;
extern uint8 bottv_flg_aviAwakeSrc;
extern uint8  bottv_flg_VIgn;

/*************************************************************
 * Call back functions for OsekNm Module
 ************************************************************/
/*Called only when NM started*/
void ApplNmCanNormal(uint8 ControllerId)
{
    gNm_SleepFlag = NOBUSSLEEP;
    gCanNm_CanRxAppFlag = CAN_START ;
    TAppMessageDelyTimer = 8;//8 ; /****uint 10ms***/

}
/*************************************************************/
/*Perform when enter limphome mode*/
void ApplNmCanLimpHomeStart(uint8 ControllerId)
{
  
}
/*Perform when Terror TimeOut.*/
void ApplNmLimpHomeState(uint8 ControllerId)
{
   
}
/*Perform when Exit LimpHome State*/
void ApplNmCanLimpHomeEnd(uint8 ControllerId)
{
    
}
void ApplNmBusOff(uint8 ControllerId)/*indicates bus off error occured*/
{
    gCanNm_CanTxAppFlag = CAN_STOP;
    gNmBusoffFlag = CAN_ACTIVE;
    if(nubNmBusOffCntAry[ControllerId]>8)
    {
        gNmBusOffConfirmFlag = CAN_ACTIVE;
    }
}

void ApplNmBusOffEnd(uint8 ControllerId)/*indicate end of bus off error */
{
    //gCanNm_CanTxAppFlag = CAN_START;
    //gNmBusoffFlag = CAN_INACTIVE;
    //gNmBusOffConfirmFlag = CAN_INACTIVE;
}
/*This callback function is performed when busOff recovery successed.*/
void ApplNmBusOffRecoverySuccess(uint8 ControllerId)
{
    gCanNm_CanTxAppFlag = CAN_START;
    gNmBusoffFlag = CAN_INACTIVE;
    gNmBusOffConfirmFlag = CAN_INACTIVE;
}

void ApplNmCanBusOffRecoveryFail(uint8 ControllerId)/*Slow Bus Off recovery failed*/
{

}

/*************************************************************/
/*This callback sets the communication hardware (CAN controller, bus transceiver)
to a state from which they can be woken up by the CAN bus.*/
void ApplNmCanSleep(uint8 ControllerId)
{
	gNm_SleepFlag=BUSSLEEP;
}
void ApplNmWaitBusSleep(NetworkHandleType nmChannelHandle)
{
    gNm_SleepFlag=WAITBUSSLEEP;
     
}

void ApplNmCanWakeUp(uint8 ControllerId)/*indicates can bus wake up*/
{

}
/*************************************************************/
void ApplNmWaitBusSleepCancel(uint8 ControllerId)
{
    gNm_SleepFlag=NOBUSSLEEP;
    gCanNm_CanTxAppFlag = CAN_START;
}



void CanAppMessageDelyCntFun(void)
{
    if ((TAppMessageDelyTimer != 0)/*&& (isNmTxSuccessFlg==1 &&(bottv_flg_aviAwakeSrc||bottv_localwakesignal||bottv_flg_VIgn))*/)
    {
        TAppMessageDelyTimer--;
        
        if (TAppMessageDelyTimer == 0)
        {
                //gCanNm_CanRxAppFlag = CAN_START ;
                gCanNm_CanTxAppFlag = CAN_START ;
        }
        else
        {
            /* do nothing */
        }
    }
    else
    {
        /* do nothing */
    }
}

#endif
